//
//  ChangeNicknameVC.swift
//  LeTu
//
//  Created by gy on 2023/12/7.
//

import UIKit

enum ChangeUserInfoViewType {
    case nickname
    case inviteCode
}

class ChangeNicknameVC: BaseViewController {

    private var viewModel: ChangeNicknameVM
    
    init(viewType: ChangeUserInfoViewType) {
        viewModel = ChangeNicknameVM(viewType: viewType)
        super.init()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        DispatchQueue.main.async {
            self.conformBtn.layerCornerRadius(self.conformBtn.height/2)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        
        switch viewModel.viewType {
        case .nickname:
            title = "昵称".localized
            contentTextField.text = kUserManager.userModel.nickname
            contentTextField.placeholder = "请输入昵称".localized
            conformBtn.setTitle("确定".localized)
        case .inviteCode:
            title = "激活卡".localized
            contentTextField.placeholder = "请输入激活码".localized
            conformBtn.setTitle("激活".localized)
        }
        
    }
    
    // MARK: - UIButtonAction
    @objc func conformBtnAction() {
        
        guard let content = contentTextField.text, content.count > 0 else { return showMessage(message: contentTextField.placeholder!)}
        
        switch viewModel.viewType {
        case .nickname:
            viewModel.modifyNickname(content) { [weak self] dialog in
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.hideIndicator()
                    strongSelf.showMessage(message: dialog.message)
                    if dialog.code == 200 {
                        strongSelf.backAction()
                    }
                }
            }
        case .inviteCode:
            viewModel.redemptionCode(content) {  [weak self] dialog in
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.hideIndicator()
                    strongSelf.showMessage(message: dialog.message)
                    if dialog.code == 200 {
                        strongSelf.backAction()
                    }
                }
            }
        }
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        navIsHidden = false
        
        view.addSubview(thickLineView)
        view.addSubview(contentTextField)
        view.addSubview(lineView)
        view.addSubview(conformBtn)
        
        thickLineView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(10)
        }
        contentTextField.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.height.equalTo(68)
            make.top.equalTo(thickLineView.snp.bottom)
        }
        lineView.snp.makeConstraints { make in
            make.left.right.equalTo(contentTextField)
            make.height.equalTo(CGFloat.app_lineHieght)
            make.top.equalTo(contentTextField.snp.bottom)
        }
        conformBtn.snp.makeConstraints { make in
            make.left.equalTo(42)
            make.right.equalTo(-42)
            make.top.equalTo(lineView.snp.bottom).offset(42)
            make.height.equalTo(CGFloat.app_btnHeight)
        }
        
        conformBtn.addTarget(self, action: #selector(conformBtnAction))
    }
    private lazy var thickLineView: UIView = {
        let thickLineView = UIView()
        thickLineView.backgroundColor = .app_viewBackgroundColor
        return thickLineView
    }()
    private lazy var contentTextField: UITextField = {
        let contentTextField = UITextField()
        contentTextField.font = kSetPingFangRegular(15)
        return contentTextField
    }()
    private lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = .app_lineColor
        return lineView
    }()
    private lazy var conformBtn: GradientButton = {
        let conformBtn = GradientButton()
        conformBtn.setTitle("确定".localized)
        conformBtn.gradientColors = UIColor.app_themeGradientColors
        conformBtn.setFont(kSetPingFangBlod(15))
        return conformBtn
    }()
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
